﻿using DroidLord.app.dy;
using DroidLord.Core;
using DroidLord.data;
using DroidLord.Task;
using DroidLord.Util;
using SharpAdbClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DroidLord
{
    public partial class ScreenFormControl : Form
    {

        SlaveViewer view;

        DeviceData device = null;

        DevScreenInfo screenInfo = new DevScreenInfo();

        GlobalEventHandler disconnectHandler = null;

        HashSet<string> ksLiveInfo = new HashSet<string>();
        HashSet<string> dyLiveInfo = new HashSet<string>();


        public ScreenFormControl()
        {
            InitializeComponent();

            view = new SlaveViewer();
            view.isControlViewer = true;
            view.setConnectStatusControlVisible(false);
            view.ResponseClick = true;
            view.Dock = DockStyle.Fill;
            view.screenFormControl = this;
            panelControl.Controls.Add(view);
        }
        public void Display(Slavery.Slave sl)
        {

            device = sl.Device;

            this.Text = "当前设备:" + device.Serial;
            view.Left = panelControl.Left;
            view.Top = panelControl.Top;
            view.Width = panelControl.Width;
            view.Height = panelControl.Height;

            view.BringToFront();

            view.Display(sl);
            alphaBlendTextBoxInputText.Parent = view;

            panelControl.Invalidate();
            view.BringToFront();
            view.Invalidate();
            //this.Invalidate();
            screenInfo.width = sl.ScreenService.Width;
            screenInfo.height = sl.ScreenService.Height;
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.Keypress(Slavery.DroidKey.BACK);
                    }
                }
                return;
            }
            if (DeviceManager.isDeviceOnline(device.Serial))
            {
                view.GetSlave().Manipulation.Keypress(Slavery.DroidKey.BACK);
            }
            else
            {
                // MessageBox.Show("当前设备已经离线,不能操作");
            }
        }

        private void buttonHome_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.Keypress(Slavery.DroidKey.HOME);
                    }
                }
            }
            else
            {
                if (DeviceManager.isDeviceOnline(device.Serial))
                {
                    view.GetSlave().Manipulation.Keypress(Slavery.DroidKey.HOME);
                }
            }
        }

        private void buttonPower_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.Keypress(Slavery.DroidKey.POWER);
                    }
                }
                return;
            }
            if (DeviceManager.isDeviceOnline(device.Serial))
            {
                view.GetSlave().Manipulation.Keypress(Slavery.DroidKey.POWER);
            }
        }

        private void ScreenFormControl_Load(object sender, EventArgs e)
        {
            alphaBlendTextBoxInputText.BackColor = Color.White;

            timerScanDeviceStatus.Start();

            ksLiveInfo = FileUtils.readLineFile(PathManager.getCacheKSLiveInfoPath());
            dyLiveInfo = FileUtils.readLineFile(PathManager.getCacheDYLiveInfoPath());
            //MessageBox.Show(ksLiveInfo.ToString());
            foreach (string ksinfo in ksLiveInfo)
            {
                string[] myinfos = ksinfo.Split(new char[] { '|' });
                if (myinfos != null && myinfos.Length == 2)
                {
                   
                }
            }

            foreach (string ksinfo in dyLiveInfo)
            {
                string[] myinfos = ksinfo.Split(new char[] { '|' });
                if (myinfos != null && myinfos.Length == 2)
                {
                   
                }
            }

           

        }

        private void showInputMethod(Point showPoint, Boolean isUsePoint)
        {
            if (isUsePoint)
            {
                alphaBlendTextBoxInputText.Visible = true;
                alphaBlendTextBoxInputText.Location = showPoint;
                alphaBlendTextBoxInputText.Focus();
                return;
            }
            else
            {
                alphaBlendTextBoxInputText.Visible = false;
            }
        }

        private void alphaBlendTextBoxInputText_TextChanged(object sender, EventArgs e)
        {
            String text = alphaBlendTextBoxInputText.Text + "";
            if (!text.Equals(""))
            {
                //screencommandQueue.Enqueue("am|broadcast|-a|ADB_INPUT_TEXT|--es|msg|" + text.Trim() + "\n");
                // alphaBlendTextBoxInputText.Text = "";
                //  alphaBlendTextBoxInputText.SelectionStart = 0;
            }
        }

        private void alphaBlendTextBoxInputText_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                String text = alphaBlendTextBoxInputText.Text + "";
                if (!text.Equals(""))
                {
                    if (device != null)
                    {
                        if (Program.mainFrom.checkBoxControlAll.Checked)
                        {
                            foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                            {
                                if (v is SlaveViewer)
                                {
                                    var view = v as SlaveViewer;
                                    view.GetSlave().Manipulation.inputText(text);
                                }
                            }

                        }
                        else
                        {
                            view.GetSlave().Manipulation.inputText(text);
                        }


                    }
                    //screencommandQueue.Enqueue("am|broadcast|-a|ADB_INPUT_TEXT|--es|msg|" + text.Trim() + "\n");
                    alphaBlendTextBoxInputText.Text = "";
                    alphaBlendTextBoxInputText.SelectionStart = 0;
                }
            }
        }

        public void panelControl_MouseMove(object sender, MouseEventArgs e)
        {
            //MessageBox.Show("fuck xxx 123");
            showInputMethod(new Point(e.X + 20, e.Y + 20), true);
        }
        private void slview_MouseMove(object sender, MouseEventArgs e)
        {
            //MessageBox.Show("fuck xxx");
            showInputMethod(new Point(e.X, e.Y), true);
        }

        private void buttonClearText_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.ClearText();
                    }
                }
                return;
            }
            if (DeviceManager.isDeviceOnline(device.Serial))
            {
                view.GetSlave().Manipulation.ClearText();
            }
        }

        private void buttonLeft_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.buttonLeft();
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.buttonLeft();
        }

        private void buttonUp_Click(object sender, EventArgs e)
        {

            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.buttonUp();
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.buttonUp();
        }

        private void buttonRight_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.buttonRight();
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.buttonRight();
        }

        private void buttonDown_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.buttonDown();
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.buttonDown();
        }

        private void ScreenFormControl_FormClosed(object sender, FormClosedEventArgs e)
        {
            view.removeFrameArrived();
            // Program.Events.UnRegister("adb_disconnect", disconnectHandler);
        }

        private void timerScanDeviceStatus_Tick(object sender, EventArgs e)
        {
            String sn = device.Serial;
            if (!DeviceManager.isDeviceOnline(sn))
            {
                timerScanDeviceStatus.Stop();
                DialogResult result = MessageBox.Show("设备:" + sn + "掉线,是否关闭当前界面", " 设备掉线", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                if (result == DialogResult.OK)
                {
                    Close();
                }
                else
                {

                }
            }
        }

        private void buttonWakeup_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.wakeUp();
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.wakeUp();
        }

       

        //private void checkBoxControlAll_CheckedChanged(object sender, EventArgs e)
        //{
        //    Program.isOpenTotalControl = Program.mainFrom.checkBoxControlAll.Checked;
        //}

       
      
        private void buttonInputText_Click(object sender, EventArgs e)
        {
            String text = textBoxInputText.Text + "";
            if (!text.Equals(""))
            {
                if (device != null)
                {
                    if (Program.mainFrom.checkBoxControlAll.Checked)
                    {
                        foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                        {
                            if (v is SlaveViewer)
                            {
                                var view = v as SlaveViewer;
                                view.GetSlave().Manipulation.inputText(text);
                            }
                        }

                    }
                    else
                    {
                        view.GetSlave().Manipulation.inputText(text);
                    }


                }
                //screencommandQueue.Enqueue("am|broadcast|-a|ADB_INPUT_TEXT|--es|msg|" + text.Trim() + "\n");
                textBoxInputText.Text = "";
                textBoxInputText.SelectionStart = 0;
            }
        }

       


       

        private void buttonSwitchIme_Click(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.switchIme();
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.switchIme();
        }

        private void buttonstopKS_Click_1(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.stopApp("com.smile.gifmaker");
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.stopApp("com.smile.gifmaker");
        }

        private void buttonstartKS_Click_1(object sender, EventArgs e)
        {
            if (Program.mainFrom.checkBoxControlAll.Checked)
            {
                foreach (var v in MainForm.mainForm.layoutPanel.Controls)
                {
                    if (v is SlaveViewer)
                    {
                        var view = v as SlaveViewer;
                        view.GetSlave().Manipulation.startAppFromMonkey("com.smile.gifmaker");
                    }
                }
                return;
            }
            view.GetSlave().Manipulation.startAppFromMonkey("com.smile.gifmaker");
        }

        private void panelControl_SizeChanged(object sender, EventArgs e)
        {
            view.Width = panelControl.Width;
            view.Height = panelControl.Height;
        }

        private void buttonDumpUI_Click(object sender, EventArgs e)
        {
            view.GetSlave().Manipulation.startDumpUI();
        }
    }
}
